package com.itheima04;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;


public class Test05 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入路径:");
        String path = sc.nextLine();


        File file = new File(path);
        if (!file.exists()) {
            System.out.println("您输入的文件夹的路径不存在!");
            return;
        } else if (file.isFile()) {
            System.out.println("您输入的是文件，而不是文件夹");
            return;
        } else {
            ArrayList<File> dirlist = new ArrayList<>();
            ArrayList<File> files = new ArrayList<>();

            diGui(file, dirlist, files);
            TreeMap<String, String> mp = new TreeMap<>();
            for (int i = 0; i < 10; i++) {
                File file1 = files.get(i);
                long haomiao = file1.lastModified();
                SimpleDateFormat sdf = new SimpleDateFormat();
                String format = sdf.format(new Date(haomiao));
                mp.put(format, file1.getPath());

            }
            mp.entrySet().forEach(s -> System.out.println("修改时间:" + s.getKey() + "-----文件路径:" + s.getValue()));
//            TreeMap<String, String> mp = new TreeMap<>();
//            for (File file1 :files) {
//                long haomiao = file1.lastModified();
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//                String format = sdf.format(new Date(haomiao));
//                String a = file1.getPath();
//                mp.put(format, a);
//
//            }
//            mp.entrySet().stream().limit(10).forEach(s-> System.out.println(s));
        }

    }

    public static void diGui(File file, ArrayList<File> dirlist, ArrayList<File> files) {
        if (file.isFile()) {
            files.add(file);
        }
        if (file.isDirectory()) {
            dirlist.add(file);
            File[] files1 = file.listFiles();
            if (files1 != null) {
                for (File chlid : files1) {
                    diGui(chlid, dirlist, files);
                }
            }
        }
    }
}
